--- 道胜/海峰 热量表
module(..., package.seeall)

local logtag = "t3-2"
local dtu, udtu = {}, {}
local isIns = false --先读取累计流量
local val,ins,addr =0,0,1
function setDtu(_dtu, _udtu)
    dtu, udtu = _dtu, _udtu
    addr = udtu.addr and tonumber(udtu.addr) or 1
    if udtu and tonumber(udtu.dtype) == 13 then sys.taskInit(init) end
end
local function _pro(data)
    local v_hex = data:sub(6, 7)..data:sub(4, 5)
    if isIns then
        local _t, _v = pack.unpack(v_hex, ">f")
        ins = _v and string.format("%.2f",_v)
        lcd.LCD_DATA[3]= "瞬时:"..ins
        isIns = false
    else
        local _t, _v = pack.unpack(v_hex, ">I")
        lcd.LCD_DATA[3]= "累计:".._v
        val = _v
        isIns = true
    end
    usrdata.update({val,ins})
end
function init()
    power.openBoost()
    sys.wait(200)
	while true do
        if isIns then
            tools.modbus_send(3,addr,3,0,2)
        else
            tools.modbus_send(3,addr,3,24,2)
        end
        local result, d = sys.waitUntil("UART_READ_DONE_NEXT_3" , 5000)
        if result then
            if d then
                _pro(d)
            end
            sys.wait(2000)
        else
            lcd.LCD_DATA[3] = "t3热 READ TIMEOUT"
        end
	end
end
